((w, d) => {
	function pupopBox() {
		return {
			show: (el) => {
				$(el).addClass("left-bottom-pupop-box");
				$(".bg-2").show();
			},
			hide: (el) => {
				$(el).removeClass("left-bottom-pupop-box");
				$(".bg-2").hide();
			}
		}
	}
	w.pupopBox = pupopBox;

})(window, document);

var host = "http://api.bailitec.com/wechat/v1/";
var pupopBox = new pupopBox();


//获取参数
function GetRequest() {   
   var url = location.search; //获取url中"?"符后的字串   
   var theRequest = new Object();   
   if (url.indexOf("?") != -1) {   
      var str = url.substr(1);   
      strs = str.split("&");   
      for(var i = 0; i < strs.length; i ++) {   
         theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);   
      }   
   }   
   return theRequest;   
}   



// 中奖信息
function winningInfo() {
	$.ajax({
		type: "get",
		url: host + "/index/autoprice",
		dataType: "JSON",
		success: function(res) {
			if(res.code == 1) {
				var Element = "",
					data = res.data;
				for(var i = 0; i < data.length; i++) {
                  var temp_name = data[i].name.substring(1);

					Element +=
						`<div class="swiper-slide"><img src="img/laba.png" class="hint-icon" />
									恭喜 <span class="name">${data[i].mobile}</span>
									成功抽到<span class="pump-prize">${data[i].title}</span>
									</div>`;
				}
             	 	
				$(".swiper-wrapper").html(Element);

				var mySwiper = new Swiper('.winning-information', {
					direction: 'vertical',
					autoplay: {
						delay: 2000
					},
					loop: true,
					pagination: {
						el: '.swiper-pagination',
						clickable: true,
					},
				});
			}
		}
	});
}
winningInfo();

// 获取奖品
var prizes = [];
var url_param = GetRequest();
function getPrize() {
	$.ajax({
		type: "get",
		url: host + "/index/getprize",
		data:{
			id:url_param.e ? url_param.e : null
		},
		dataType: "JSON",
		success: function(res) {

			$('body').addClass('loaded');
    		$('#loader-wrapper .load_title').remove();
			if(res.code == 1) {
				var prize = res.data.prize,
					prize_element = "";
				for(var i = 0; i < prize.length; i++) {
					if(i < 8) {
						prizes.push(prize[i]);
						prize_element += `<img src="${prize[i].src}" class="dial2-img ${'location-' + (i+1)}"/>`;
					}
				}
				$(".dial2").html(prize_element);
			}
		}
	});
}
getPrize();

//用户须知
function notice(){
	$.ajax({
		type: "get",
		url: host + "/index/getneed",
		dataType: "JSON",
		success: function(res) {
			console.log(res);
			if(res.code == 1){
				$(".notice").html(res.data.need);
			}
		}
	});
}
notice();

// 获取用户信息
var prize_status;					
function getUserInfo() {
	$.ajax({
		type: "post",
		url: host + "user/getuser",
		data: {
			token: localStorage.getItem("userInfo")
		},
		dataType: "JSON",
		success: function(res) {
			console.log(res);
			if(res.code == 1) {
				
				$(".modules-left-info,.modules-left-hint-info").hide();
				$(".count-down").text(res.data.prize_time);
				prize_status = res.data.prize_time;
				
				if(res.data.is_prize == 1) {
					$(".Prompt").show();
				}
				if(res.data.num > 0) {
					
					$(".Progress").show();
					$(".Progress").css("height", parseInt(res.data.num * 0.05) + "rem");
					$(".Progress-num span").text(res.data.num);
				}
				
			}
		}
	});
}
getUserInfo();

// 点击抽奖
$(".dalor-button").bind("click", active = function() {
	var userInfo = localStorage.getItem("userInfo"),
		consent = $(".checkbox-icon2:visible").data("consent");
	if(!userInfo) {
		pupopBox.show(".login");
	} else if(!consent) {
		alert("请阅读用户须知");
	} else {
		if(prize_status == 0){
			pupopBox.show(".add-prize");
		}else{
			$(this).unbind();
			rotate();
		}
		
	}
});

// 是否同意抽奖规则
$(".input-checkbox2").on("click", function() {
	var consent = $(".checkbox-icon2:visible").data("consent");
	if(consent) {
		$(".checkbox-icon2").hide();
	} else {
		$(".checkbox-icon2").show();
	}
});

var original_drawing = [
	"img/Lucky_turntable_bulb1.png",
	"img/Lucky_turntable_bulb2.png",
	"img/Lucky_turntable_bulb3.png",
	"img/Lucky_turntable_bulb4.png",
	"img/Lucky_turntable_bulb5.png",
	"img/Lucky_turntable_bulb6.png",
	"img/Lucky_turntable_bulb7.png",
	"img/Lucky_turntable_bulb8.png",
	"img/Lucky_turntable_bulb9.png",
	"img/Lucky_turntable_bulb10.png",
];
var active_img = [
	"img/Lucky_turntable_bulb_light1.png",
	"img/Lucky_turntable_bulb_light2.png",
	"img/Lucky_turntable_bulb_light3.png",
	"img/Lucky_turntable_bulb_light4.png",
	"img/Lucky_turntable_bulb_light5.png",
	"img/Lucky_turntable_bulb_light6.png",
	"img/Lucky_turntable_bulb_light7.png",
	"img/Lucky_turntable_bulb_light8.png",
	"img/Lucky_turntable_bulb_light9.png",
	"img/Lucky_turntable_bulb_light10.png"
];
var deg = 45, //奖品角度
	rotate_nums = 6, //旋转次数
	active_deg = 360 * rotate_nums, //旋转度数
	winning_imgSrc = "";
	
// 旋转
function rotate() {
	$.ajax({
		type: "post",
		url: host + "user/prize",
		data: {
			token: localStorage.getItem("userInfo")
		},
		dataType: "JSON",
		success: function(res) {
			console.log(res);
			if(res.code == 1) {
				var data = res.data;
				for(var i = 0; i < prizes.length; i++) {
					if(data.id == prizes[i].id) {
						$(".arrows").attr("style", "transform:rotate(-45deg);");
						$(".dial2").attr("style", "transform:rotate(" + (active_deg - deg * i) + "deg);transition: all 2.6s;");
						active_deg += active_deg;
						winning_imgSrc = prizes[i].src;
						curve();
					}
				}
			}else if(res.code == 1002){
				alert(res.msg);
				pupopBox.show(".info");
				$(".dalor-button").bind("click", active);

			}else {
				alert(res.msg);
				$(".dalor-button").bind("click", active);
			}
		}
	})
}


// 曲线
var counter = 50,
	imgItem = -1,
	interval;

function curve() {
	clearInterval(interval);
	counter++;
	if(counter < 88) {
		interval = setInterval(curve, counter);
		imgItem >= 10 ? imgItem = -1 : imgItem++;
		$(".dial .Lucky_turntable_bulb").eq(imgItem).attr("src", active_img[imgItem]);
		$(".dial .Lucky_turntable_bulb").eq(imgItem - 1).attr("src", original_drawing[imgItem - 1]);
	} else {
		counter = 50, imgItem = -1;
		getUserInfo();
		$(".arrows").attr("style", "transform:rotate(-15deg);");
		$(".winning-present").show();
		$(".winning-present-img").attr("src", winning_imgSrc);
		
		setTimeout(function() {
			$(".winning-present").hide();
			$(".dalor-button").bind("click", active);
		}, 3000);
	}
}

//登录状态 
function loginStatus() {
	var userInfo = localStorage.getItem("userInfo");
	if(!userInfo) {
		return false
	}
}
// 点击显示登录
$(".modules-left").on("click", function() {
	$.ajax({
		type: "post",
		url: host + "user/getuser",
		data: {
			token: localStorage.getItem("userInfo")
		},
		dataType: "JSON",
		success: function(res) {
			if(res.code == 1) {
				alert("您已登录");
			} else {
				pupopBox.show(".login");
			}
		}
	});

});

// 获取验证码
$(".get-verification-code").on("click", getVerificationCode = function() {

	var mobile_val = $(".mobile").val(),_this = $(this);
	if(mobile_val.length == 0) {

		alert("请输入手机号");

	} else if(mobile_val.length > 0 && mobile_val.length < 11) {

		alert("请输入正确的手机号");

	} else {

		_this.unbind();
		_this.css("background-image", "url(img/btn.png)");
		
		var num = 60;
		var inter = setInterval(function() {
			num--;
			_this.text(num + 's');
			if(num <= 0) {
				_this.css("background-image", "url(img/Button_go.png)");
				clearInterval(inter);
				_this.text("发送验证码");
				_this.bind("click", getVerificationCode);
			}
		}, 1000);

		$.ajax({
			type: "POST",
			url: host + 'index/sendmsg',
			data: {
				mobile: mobile_val
			},
			dataType: "JSON",
			success: function(res) {
				if(res.code != 1) {
					alert(res.msg)
				}
			}
		});
	}
});

// 登录
$(".login-button").on("click", function() {
	var mobile_val = $(".mobile").val(),
		captcha = $(".verification-code").val(),
		_this = $(this);
	if(mobile_val.length == 0) {
		alert("请输入手机号");
	} else if(captcha.length == 0) {
		alert("请输入验证码");
	} else {
		$.ajax({
			type: "post",
			url: host + "index/login",
			data: {
				mobile: mobile_val,
				captcha: captcha
			},
			dataType: "JSON",
			success: function(res) {
				console.log(res);
				if(res.code == 1) {
					localStorage.setItem("userInfo", res.data.token);
					getUserInfo();
					pupopBox.hide(".login");
					alert(res.msg);
				}else if(res.code == 1002) {
					localStorage.setItem("userInfo", res.data.token);
					getUserInfo();
					pupopBox.hide(".login");
					pupopBox.show(".info");
				}else{
					alert(res.msg);
				}
			}
		});
	}
});

//我的奖品
$(".my-prize").on("click", function() {
	if(loginStatus() != false) {
		myPrize();
	} else {
		pupopBox.show(".login");
	}

});

var prize_array = [];

function myPrize() {
	$.ajax({
		type: "post",
		url: host + "user/myprize",
		data: {
			token: localStorage.getItem("userInfo")
		},
		dataType: "JSON",
		success: function(res) {
			console.log(res);
			if(res.code == 1) {
				pupopBox.show(".prize");
				
				if(res.data.gift.length > 0 || res.data.prize.length > 0) {
					var Element = "",data = res.data.prize.concat(res.data.gift);

					for(var i = 0; i < data.length; i++) {
						prize_array.push(data[i]);
						if(data[i].key  != "") {
							Element += `<div class="scroll-item">
								<div class="get-prize-1">
									<div class="info-1">
										<img src="${data[i].src}" class="info-1-img" />
										<span class="info-1-text">${data[i].key}</span>
									</div>
									<p class="info-2">有效期:2018.6.30</p>
								</div>
								<div class="get-prize-button-1" data-prizestatus="${0}" data-id="${data[i].id}">
									${data[i].status == 0 ?'及时兑换' : '及时兑换'}
								</div>
							</div>`;
						} else {
							Element += `<div class="scroll-item">
									<div class="get-prize-2">
										<img src="${data[i].src}" class="prize-2-img" />
										<div class="prize-2-info">
											<p class="prize-2-info-1">${data[i].title}</p>
											<p class="prize-2-info-2">7日内发出</p>
										</div>
									</div>
									<div class="get-prize-button-2" data-prizestatus="${data[i].status}" data-id="${data[i].id}">
										${data[i].status == 0 ?'填写地址' : '已领取'}
										</div>
								</div>`;
						}
					}
					$(".scroll-view").html(Element);
				} else {
					$(".scroll-view").html("<p style='text-align:center;font-size:.3rem;color:#8D5525;'>暂无奖品</p>");
				}
			} else {
				pupopBox.show(".login");
			}
		}
	});
}

//领取奖品
$(".scroll-view").on("click", ".get-prize-button-1", function() {
	var prizeStatus = $(this).data("prizestatus"),
		id = $(this).data("id");
	if(prizeStatus == 0) {
		$.ajax({
			type: "post",
			url: host + "user/receiveprize",
			data: {
				id: id,
				token: localStorage.getItem("userInfo")
			},
			dataType: "JSON",
			success: function(res) {
				if(res.code == 1) {
					myPrize();
					alert("CDKey已复制到剪贴板，请打开梦幻花园（Android）游戏首页，点击“设置”—“兑换码”即可兑换福利！");
				}
			}
		});
	}
});

//领取奖品 
var prize_id;
$(".scroll-view").on("click", ".get-prize-button-2", function() {
	var prizeStatus = $(this).data("prizestatus");
	prize_id = $(this).data("id");
	if(prizeStatus == 0) {
		$(".bg-3").show();
		$(".fill-an-address").addClass("left-bottom-pupop-box");
	}
});

//填写地址
$("body").on("click", " .site", Site = function(id) {
	var address = $(".textarea").val();
	if(address.length == 0) {
		alert("请填输入地址");
	} else {
		$.ajax({
			type: "post",
			url: host + "user/receiveprize",
			data: {
				id: prize_id,
				address: address,
				token: localStorage.getItem("userInfo")
			},
			dataType: "JSON",
			success: function(res) {
				if(res.code == 1) {
					myPrize();
					$(".bg-3").hide();
					$(".fill-an-address").removeClass("left-bottom-pupop-box");
					alert(res.msg);
				}
			}
		});
	}
});

//填写信息
$(".info-label").on("click", function() {
	var item = $(this).index();
	$(".sex").hide();
	$(".sex").eq(item - 1).show();
});

$(".info-button").on("click", function() {
	var user_name = $(".user-name").val(),
		date = $(".date").val(),
		sex = $(".sex:visible").data("sex");
	if(user_name.legnth == 0) {
		alert("请输入姓名");
	} else if(date.length == 0) {
		alert("请选择生日");
	} else {
		$.ajax({
			type: "post",
			url: host + "user/upuser",
			data: {
				name: user_name,
				sex: sex,
				birthday: date,
				token: localStorage.getItem("userInfo")
			},
			dataType: "JSON",
			success: function(res) {
				if(res.code == 1) {
					alert(res.msg);
					pupopBox.hide(".info");
				} else {
					alert(res.msg);
				}
			}
		});
	}
});

//查看礼品详情
$(".view-prize-detail").on("click", function() {
	pupopBox.hide(".prize");
	pupopBox.show(".details");
	$.ajax({
		type: "get",
		url: host + "index/getprize",
		
		dataType: "JSON",
		success: function(res) {
			if(res.code == 1) {
				if(res.data) {
					var Element = "",data = res.data.prize.concat(res.data.gift);
					for(var i = 0; i < data.length; i++) {
						Element += `<div class="details-scroll-item">
							<div class="frist">
								<img src="${data[i].src}"  class="details-icon" />
								<div class="details-info">
									<p class="details-info-title">${data[i].title}</p>
									<p class="details-info-text">${data[i].des ? data[i].des : ''}</p>
								</div>
							</div>
							<div class="child"></div>
						</div>`;
					}
					$(".details-scroll-view").html(Element);

				} else {
					$(".details-scroll-view").html("<p style='text-align:center;font-size:.3rem;color:#8D5525;'>没有奖品，快去抽奖！</p>");

				}
			}
		}
	});
});

// 退出
$(".close").on("click", function() {
	pupopBox.hide(".login,.info,.prize,.details");
});

$(".site-close").on("click", function() {
	$(".bg-3").hide();
	$(".fill-an-address").removeClass("left-bottom-pupop-box");
});

$(".add-prize-close").on("click", function() {
	pupopBox.hide(".add-prize");
});

$(".agreement-info").on("click", function() {
	pupopBox.show(".specification");
});

$(".agreement-info").on("click", function() {
	pupopBox.show(".specification");
});

$(".specification-close").on("click", function() {
	pupopBox.hide(".specification");
});

function pupopBox2() {
	return {
		show: function(el) {
			$(".bg-4").show();
			$(el).addClass("left-bottom-pupop-box");
		},
		hide: function(el) {
			$(".bg-4").hide();
			$(el).removeClass("left-bottom-pupop-box");
		}
	}
}
var pupopBox2 = new pupopBox2();

// 获取更多抽奖次数
$(".modules-center").on("click", function() {
	pupopBox.show(".add-prize");
});

//领取抽奖次数
$(".get-Lottery-button-1").on("click", function() {
	$.ajax({
		type: "post",
		url: host + "user/addtime",
		data:{
			token:localStorage.getItem("userInfo")
		},
		dataType: "JSON",
		success: function(res) {
			if(res.code == 1){
				getUserInfo();
				alert(res.msg);
			}else{
				alert(res.msg);
			}

		}
	});
});

$(".get-Lottery-button-2").on("click", function() {
	var item = $(this).data("index");
	if(item == 1) {
		pupopBox2.show(".regulation");
	} else if(item == 3) {
		pupopBox2.show(".activate");
	}
});

$(".one-card-status").on("click", function() {
	pupopBox2.show(".one");
});

$(".info-close").on("click", function() {
	pupopBox2.hide(".regulation,.activate,one");
});